// DwarfMgrBean.java // Stateless Session Bean Facade package dwarf; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import java.util.*; public class DwarfMgrBean implements SessionBean { static DwarfEntityHome getDwarfHome() { try { InitialContext jndiContext = new InitialContext(); Object ref = jndiContext.lookup("dwarf/DwarfEntity"); DwarfEntityHome home = (DwarfEntityHome)ref; return home; } catch (Exception e) { // hack throw new RuntimeException("Naming Failure: " + e.getMessage()); } } //-------------------------------------------------------------- private static Dwarf toDwarfBean(DwarfEntity dwarf){ MountainEntity dHome = dwarf.getHome(); DwarfEntity spouse = dwarf.getSpouse(); Dwarf d = new Dwarf(); d.setDwarfId(dwarf.getDwarfId().intValue()); d.setDwarfName(dwarf.getDwarfName()); d.setBorn(dwarf.getBorn()); if(dHome != null){ d.setHomeId(dHome.getMountainId().intValue()); d.setHomeName(dHome.getMountainName()); } if(spouse != null){ d.setSpouseId(spouse.getDwarfId().intValue()); d.setSpouseName(spouse.getDwarfName()); } return d; } //-------------------------------------------------------------- public List GetAll() throws javax.ejb.FinderException { DwarfEntityHome home = getDwarfHome(); java.util.Collection entityList = home.findAll(); List ret = new LinkedList(); for(Iterator i = entityList.iterator(); i.hasNext(); ){ DwarfEntity dwarf = (DwarfEntity)i.next(); Dwarf d = toDwarfBean(dwarf); ret.add(d); } return ret; } //-------------------------------------------------------------- public Dwarf Get(int id) throws javax.ejb.FinderException { DwarfEntityHome home = getDwarfHome(); DwarfEntity dwarf = home.findByPrimaryKey(new Integer(id)); if(dwarf != null){ return toDwarfBean(dwarf); }else{ return null; } } //-------------------------------------------------------------- public int Add(String dwarfName,int born,int homeId) throws javax.ejb.FinderException, javax.ejb.CreateException { DwarfEntityHome dHome = getDwarfHome(); MountainEntityHome mHome = MountainMgrBean.getMountainHome(); MountainEntity homeOfTheDwarf = null; if(homeId != 0){ homeOfTheDwarf = mHome.findByPrimaryKey(new Integer(homeId)); } DwarfEntity theD = dHome.create(dwarfName,born,homeOfTheDwarf); // work-around theD.setHome(homeOfTheDwarf); // return id later... return 0; } //-------------------------------------------------------------- public void Update(int id, String dwarfName,int born,int homeId) throws javax.ejb.FinderException { DwarfEntityHome dHome = getDwarfHome(); MountainEntityHome mHome = MountainMgrBean.getMountainHome(); MountainEntity homeOfTheDwarf = null; if(homeId != 0){ homeOfTheDwarf = mHome.findByPrimaryKey(new Integer(homeId)); } DwarfEntity theDwarf = dHome.findByPrimaryKey(new Integer(id)); theDwarf.setDwarfName(dwarfName); theDwarf.setBorn(born); theDwarf.setHome(homeOfTheDwarf); } public void ejbCreate() {} public void ejbPostCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} }